package com.czz.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.manage.entity.Teacher;
import com.czz.manage.entity.User;
import com.czz.manage.mapper.UserMapper;
import com.czz.manage.query.UserQuery;
import com.czz.manage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @ClassName: UserServiceImpl
 * @Author Czz
 * @Date 2020/2/17
 * @Time 16:16
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public void getUserPageList(Page<User> pageParam, UserQuery userQuery) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("user_status");
        queryWrapper.orderByDesc("gmt_create");
        queryWrapper.orderByAsc("is_teacher");

        if (ObjectUtils.isEmpty(userQuery)){
            userMapper.selectPage(pageParam,queryWrapper);
            return;
        }

        String mobile = userQuery.getMobile();
        String name = userQuery.getName();
        Integer teacher = userQuery.getTeacher();
        Integer status = userQuery.getStatus();
        String begin = userQuery.getBegin();
        String end = userQuery.getEnd();

        if (!StringUtils.isEmpty(mobile)){
            queryWrapper.like("user_mobile",mobile);
        }

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("user_nickname",name);
        }

        if (!StringUtils.isEmpty(teacher)){
            queryWrapper.eq("is_teacher",teacher);
        }

        if (!StringUtils.isEmpty(status)){
            queryWrapper.eq("user_status",status);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }

        userMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public void updateUserStatus(String id, Integer status) {
        User user = new User();
        user.setId(id).setStatus(status);
        userMapper.updateById(user);
    }
}
